<template>
  <el-form :model="form" label-width="120px" ref="form" :rules="rules" @submit.native.prevent>
    <el-form-item label="姓名" prop="name" required>
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender" required>
      <el-select v-model="form.gender">
        <el-option label="男" value="男"></el-option>
        <el-option label="女" value="女"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="年龄" prop="age" required>
      <el-select v-model="form.age" placeholder="请选择年龄">
        <el-option
            v-for="age in ageOptions"
            :key="age"
            :label="age"
            :value="age">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="图片" prop="picture" required>
      <el-upload
          class="avatar-uploader"
          action="http://localhost:8080/resume/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"

      >
        <img v-if="imageUrl" :src="this.imageUrl" class="avatar" alt="头像">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <!-- <p>当前图片地址：{{ imageUrl }}</p> -->
    <el-form-item label="电话号码" prop="phoneNumber" required>
      <el-input v-model="form.phoneNumber"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email" required>
      <el-input v-model="form.email"></el-input>
    </el-form-item>
    <el-form-item label="求职意向" prop="intention" required>
      <el-input v-model="form.intention"></el-input>
    </el-form-item>
    <el-form-item label="能力" prop="abilities" required>
      <el-input type="textarea" :rows="3.8" v-model="form.abilities"></el-input>
    </el-form-item>
    <el-form-item label="教育背景" prop="background" required>
      <el-input type="textarea" :rows="3.8" v-model="form.background" :placeholder="backgroundPlaceholder">
      </el-input>
    </el-form-item>
    <el-form-item label="工作经验" prop="experience" required>
      <el-input type="textarea" :rows="3.8" v-model="form.experience" :placeholder="experiencePlaceholder">
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="submitForm">提交并生成模板</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import axios from 'axios';
import router from '@/router';

export default {
  data() {
    return {
      form: {
        name: '',
        gender: '未填写',
        age: null,
        picture: '',
        phoneNumber: '',
        email: '',
        intention: '',
        abilities: '',
        background: '',
        experience: '',
      },
      imageUrl: '',
      ageOptions: Array.from({length: 53}, (_, i) => i + 18),
      rules: {
        name: [
          {required: true, message: '请填写姓名', trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
        age: [
          {required: true, message: '请选择年龄', trigger: 'change'}
        ],
        picture: [
          {required: true, message: '请上传图片', trigger: 'blur'}
        ],
        phoneNumber: [
          {required: true, message: '请填写电话号码', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请填写邮箱', trigger: 'blur'}
        ],
        intention: [
          {required: true, message: '请填写求职意向', trigger: 'blur'}
        ],
        abilities: [
          {required: true, message: '请填写个人能力', trigger: 'blur'}
        ],
        background: [
          {required: true, message: '请填写教育背景，格式为院校-学历，如有多个院校则换行', trigger: 'blur'}
        ],
        experience: [
          {required: true, message: '请填写工作经历，格式为公司（换行）职位（换行）工作任务', trigger: 'blur'}
        ]
      },
      backgroundPlaceholder: '格式为院校-学历，如有多个院校则换行',
      experiencePlaceholder: '格式为公司（换行）职位（换行）工作任务'
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res.data);
      this.imageUrl = res.data;
      this.form.picture = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPGorPNG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPGorPNG) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPGorPNG && isLt2M;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let sendData = {
            resume: this.form,
            userId: this.$store.state.id,
          }
          console.log(sendData)

          axios.post('http://localhost:8080/resume/upload_resume', sendData)
              .then((response) => {
                console.log('表单数据提交成功，后端响应：', response);
                const id = response.data.data;
                console.log("这是id " + id);

                this.$message({
                  message: '表单数据提交成功！',
                  type: 'success',
                  duration: 2000
                });
                this.$emit('formUpdated');
              })
              .catch((error) => {
                console.error('表单数据提交失败，错误信息：', error);
                this.$message({
                  message: '表单数据提交失败，请检查网络或稍后重试！',
                  type: 'error',
                  duration: 2000
                });
              });
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.imageUrl = '';
    }
  }
};
</script>

<style scoped>
.avatar-uploader.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader.el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 132px;
  text-align: center;
}

.avatar {
  width: 120px;
  height: 120px;
  display: block;
}

.el-textarea {
  width: 400px;
}

.el-input {
  width: 400px;
}
</style>
